﻿package  
{
	
	import caurina.transitions.Tweener;
	import com.saint.utils.Logger;
	import com.sonishpere.FlashVarsHolder;
	import com.sonishpere.IApplication;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author kembera
	 */
	public class LoaderMain extends Sprite
	{
		
		private var loader:Loader = new Loader();
		private var loadDisplay:MovieClip;
		
		/**
		 * CONSTRUCTOR
		 */
		public function LoaderMain() 
		{
			Logger.info("LoaderMain.LoaderMain");
			
			Logger.setLogLevel(Logger.DEBUG);
			
			/* set up the functions for re-sizing */			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;1
			
			/* add the load display clip from the library */
			loadDisplay = new LoadDisplay();
			loadDisplay.x = stage.stageWidth / 2;
			loadDisplay.y = stage.stageHeight / 2;
			loadDisplay.animation.alpha = 0.8;
			addChild(loadDisplay);
			
			getFlashVars();
			
			/* set up laoder and load main app */
			var url:String = "Teaser_page.swf";			
			var urlReq:URLRequest = new URLRequest(url);			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, done);			
			loader.load(urlReq);
			
			
		}
		
		private function getFlashVars():void
		{
			Logger.debug("LoaderMain.getFlashVars " + root.loaderInfo.parameters);
		
			var val:Boolean;
			for (var fv:String in root.loaderInfo.parameters)
			{
				trace(fv + " = " + root.loaderInfo.parameters[fv]);
				val = root.loaderInfo.parameters[fv];
				
				
			
			}
			var flashVars:Object = root.loaderInfo.parameters
			
			
			FlashVarsHolder.getInstance().UK_LIVE = flashVars.uk == "true";
			FlashVarsHolder.getInstance().TRK_LIVE = flashVars.trk == "true";
			FlashVarsHolder.getInstance().SWZ_LIVE = flashVars.swz == "true";
			FlashVarsHolder.getInstance().SWE_LIVE = flashVars.swe == "true";
			FlashVarsHolder.getInstance().ROM_LIVE = flashVars.rom == "true";
			FlashVarsHolder.getInstance().POL_LIVE = flashVars.pol == "true";
			FlashVarsHolder.getInstance().GRE_LIVE = flashVars.gre == "true";
			FlashVarsHolder.getInstance().FIN_LIVE = flashVars.fin == "true";
			FlashVarsHolder.getInstance().ESP_LIVE = flashVars.esp == "true";
			FlashVarsHolder.getInstance().CZEC_LIVE = flashVars.czec == "true";
			FlashVarsHolder.getInstance().BUL_LIVE = flashVars.bul == "true";
			
			
			
		}
		
		
		
		/**
		 * WHEN LOADED ADD MAIN MOVIE AND FADE IN
		 * @param	e
		 */
		private function done(e:Event):void 
		{
			Logger.info("LoaderMain.LoaderMain " + loader.content);
			
			loader.alpha = 0;
			addChild(loader);			
			IApplication(loader.content).init();			
			Tweener.addTween(loader, { alpha:1, time:7 } );
			Tweener.addTween(loadDisplay, { alpha:0, time:7 } );			
			
		}
		
		
		function loop(e:ProgressEvent):void
		{
			var perc:Number = e.bytesLoaded / e.bytesTotal;
			var s:String = Math.ceil(perc*100).toString()+"%";
			//Logger.debug("LoaderMain.loop progress: " + s);
			loadDisplay.percentage.text = s;
			loadDisplay.animation.fade.rotation += 1;
		}
		
		
	}
	
}